<?php
/**
 * 
 * 操作分类
 * 
 * @category System
 * @author Steve Liu
 * @copyright Eshifang 
 */

class Web_Model_Class
{
	/**
	 * 默认数据库名
	 * 
	 * @var string
	 */
    private static $_table = 'class';
    
    /**
     * 存储排序后分类
     * 
     * @var array
     */
    private static $_class = array();
    
    /**
     * 初始化分类数组
     */
    private static function _init()
    {
        if(!empty(self::$_class)){
            return;
        } 
        $class = Eren_Registry::get('DB')->table(self::$_table)->orderBy('class_sort', 'ASC')->select('*');
        self::_formatClass($class);
    }
    
    /**
     * 格式化数组
     * 
     * @param array $class
     */
    private static function _formatClass($class)
    {
        if($class['count'] == 0){
            return;
        }
        foreach ($class['data'] as $each){
            if($each['class_parent_id'] == 0){
                self::$_class[$each['class_id']]['name'] = $each['class_name'];
                self::$_class[$each['class_id']]['sort'] = $each['class_sort'];
                self::$_class[$each['class_id']]['subClass'] = array();
            }
        }
        foreach ($class['data'] as $each){
            if($each['class_parent_id'] != 0){
                self::$_class[$each['class_parent_id']]['subClass'][$each['class_id']] = $each;
            }
        }
    }
    
    /**
     * 获得所有分类
     * 
     * @return array
     */
    public static function getAllClass()
    {
        self::_init();
        return self::$_class;
    }
    
    /**
     * 获得分类名
     * 
     * @param int $classID
     * @return string
     */
    public static function getClassNameByID($classID)
    {
    	$classID = intval($classID);
    	$class = Eren_Registry::get('DB')->table(self::$_table)->where('class_id', '=', $classID)->select('class_name');
    	if(isset($class['data'][0])){
    		return $class['data'][0]['class_name'];
    	}
    	return '';
    }
    
    /**
     * 校验分类ID是否合法
     * 
     * @param int $mainClassID
     * @param int $subClassID
     * @return bool
     */
    public static function checkClass($mainClassID, $subClassID)
    {
        self::_init();
        if(isset(self::$_class[$mainClassID]) && isset(self::$_class[$mainClassID]['subClass'][$subClassID])){
            return TRUE;
        }
        return FALSE;
    }
    
    /**
     * 校验分类的排序和类名是否合法
     * 
     * @param int $classSort
     * @param string $className
     * @throws Eren_Exception
     */
    private static function _check(&$classSort, &$className)
    {
        if($classSort < 0){
            throw new Eren_Exception('分类的排序必须大于等于0的整数', -1);
        }else if($className == ''){
            throw new Eren_Exception('分类名不能为空', -1);
        }
        return TRUE;
    }
    
    /**
     * 更新分类
     * 
     * @param int $classID
     * @param int $classSort
     * @param string $className
     */
    public static function updateClass($classID, $classSort, $className)
    {
        $className = trim($className);
        $classSort = intval($classSort);
        $classID   = intval($classID);
        self::_check($classSort, $className);
        $fields = array(
        'class_sort'  => $classSort,
        'class_name'  => $className
        );
        Eren_Registry::get('DB')->table(self::$_table)->where('class_id', '=', $classID)->update($fields);
        Eren_Registry::get('DB')->table('article')->where('article_sub_class_id', '=', $classID)->update(array('article_sub_class_name' => $className));
        Eren_Registry::get('DB')->table('article')->where('article_main_class_id', '=', $classID)->update(array('article_main_class_name' => $className));
        return TRUE;
    }
    
    /**
     * 增加父分类
     * 
     * @param int $classSort
     * @param string $className
     */
    public static function addMainClass($classSort, $className)
    {
        $className = trim($className);
        $classSort = intval($classSort);
        self::_check($classSort, $className);
        $fields = array(
        'class_name'  =>$className,
        'class_sort'  =>$classSort,
        );
        Eren_Registry::get('DB')->table(self::$_table)->insert($fields);
        return TRUE;
    }
    
    /**
     * 增加子分类
     * 
     * @param int $classPID
     * @param int $classSort
     * @param string $className
     */
    public static function addSubClass($classPID, $classSort, $className)
    {
        $className = trim($className);
        $classSort = intval($classSort);
        $classPID   = intval($classPID);
        self::_check($classSort, $className);
        $fields = array(
        'class_parent_id' =>$classPID,
        'class_name'      =>$className,
        'class_sort'      =>$classSort
        );
        Eren_Registry::get('DB')->table(self::$_table)->insert($fields);
        return TRUE;
    }
    
    /**
     * 删除分类
     * 
     * @param int $classID
     * @throws Eren_Exception
     */
    public static function delClass($classID)
    {
        $classID   = intval($classID);
        $ret = Eren_Registry::get('DB')->table(self::$_table)->where('class_parent_id', '=', $classID)->count();
        if($ret > 0){
            throw new Eren_Exception('子分类不为空，请先删除子分类', -1);
        }
        $ret = Eren_Registry::get('DB')->table('article')
                                       ->where('article_sub_class_id', '=', $classID)
                                       ->wAnd()
                                       ->where('article_type', '=', 1)
                                       ->count();
        if($ret > 0){
            throw new Eren_Exception('分类下文章不为空，请先删除文章', -2);
        }
        Eren_Registry::get('DB')->table(self::$_table)->where('class_id', '=', $classID)->delete();
        return TRUE;
    }
}
